package ${package.Controller};

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yc.enums.StatusEnum;
import org.springframework.web.bind.annotation.RequestMapping;
import ${package.Entity}.${entity};
import ${package.Service}.${table.serviceName};
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import com.yc.api.CommonPage;
import com.yc.api.CommonResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import java.util.List;
<#if restControllerStyle>
import org.springframework.web.bind.annotation.RestController;
<#else>
import org.springframework.stereotype.Controller;
</#if>
<#if superControllerClassPackage??>
import ${superControllerClassPackage};
</#if>

/**
 * <p>
 * ${table.comment!} 前端控制器
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
<#if restControllerStyle>
@RestController
<#else>
@Controller
</#if>
@RequiredArgsConstructor
@RequestMapping("/v1<#if package.ModuleName?? && package.ModuleName != "">/${package.ModuleName}</#if>/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>")
@Api(value = "${entity}Controller", tags = "${table.comment}")
<#if kotlin>
class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}()</#if>
<#else>
<#if superControllerClass??>
public class ${table.controllerName} extends ${superControllerClass} {
<#else>
public class ${table.controllerName} {
</#if>

    private final ${table.serviceName} ${entity?uncap_first}Service;

    @PostMapping("/add")
    @ApiOperation(value = "创建${entity}",notes = "创建${entity}")
    public CommonResult<?> create(@RequestBody ${entity} ${entity?uncap_first}) {
        ${entity?uncap_first}.setId(null);
        boolean isOk = ${entity?uncap_first}Service.create(${entity?uncap_first});
        if (isOk) {
            return CommonResult.success(${entity?uncap_first});
        }
        return CommonResult.failed();
    }

    @PostMapping("/deleteByIds")
    @ApiOperation(value = "删除${entity}",notes = "删除${entity}")
    public CommonResult<?> delete(@RequestBody List<Long> ids) {
        boolean isOk = ${entity?uncap_first}Service.delete(ids);
        if (isOk) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @PostMapping("/updateById/{id}")
    @ApiOperation(value = "更新${entity}",notes = "更新${entity}")
    public CommonResult<?> update(@PathVariable("id") Long id, @RequestBody JSONObject params) {
        ${entity} ${entity?uncap_first} = JSONUtil.toBean(params, ${entity}.class);
        ${entity?uncap_first}.setId(id);
        boolean isOk = ${entity?uncap_first}Service.update(${entity?uncap_first});
        if (isOk) {
            return CommonResult.success(${entity?uncap_first});
        }
        return CommonResult.failed();
    }

    @PostMapping("/updateStatusById/{id}")
    @ApiOperation(value = "更新ProductAttribute", notes = "更新ProductAttribute")
    public CommonResult<?> updateStatusById(@PathVariable("id") Long id, @RequestBody JSONObject params) {
    StatusEnum statusEnum = params.getEnum(StatusEnum.class, "status");
        ${entity} ${entity?uncap_first} = new ${entity}();
        ${entity?uncap_first}.setId(id);
        ${entity?uncap_first}.setStatus(statusEnum);
        boolean isOk = ${entity?uncap_first}Service.update(${entity?uncap_first});
        if (isOk) {
            return CommonResult.success(${entity?uncap_first});
        }
        return CommonResult.failed();
    }

    @PostMapping("/queryById/{id}")
    @ApiOperation(value = "根据Id获取${entity}",notes = "根据Id获取${entity}")
    public CommonResult<?> queryById(@PathVariable("id") Long id){
        ${entity} ${entity?uncap_first} = ${entity?uncap_first}Service.queryById(id);
        if (${entity?uncap_first} != null){
            return CommonResult.success(${entity?uncap_first});
        }
        return CommonResult.failed();
    }

    @PostMapping("/list")
    @ApiOperation(value = "获取${entity}列表",notes = "获取${entity}列表")
    public CommonResult<?> listAll() {
        List<${entity}> list = ${entity?uncap_first}Service.selectList();
        return CommonResult.success(list);
    }

    @PostMapping("/page")
    @ApiOperation(value = "获取${entity}列表",notes = "获取${entity}列表")
    public CommonResult<?> pageAll() {
        JSONObject params = getParams();
        int pageNum = params.getInt("pageNum", 1);
        int pageSize = params.getInt("pageSize", 25);
        IPage<${entity}> page = ${entity?uncap_first}Service.selectPage(new Page<>(pageNum, pageSize));
        return CommonResult.success(CommonPage.restPage(page));
    }
}
</#if>
